<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5">
        <el-button plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['event:event:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['event:event:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['event:event:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['event:event:export']">导出</el-button>
      </el-col>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px"
        class="el-form-search">
        <el-form-item label="设备" prop="deviceId" class="el-form-search-item">
          <el-select v-model="queryParams.deviceId" placeholder="请选择设备类型" clearable filterable @change="setChannelList">
            <el-option v-for="device in deviceList" :key="device.zhGuid" :label="device.devName"
              :value="device.zhGuid" />
          </el-select>
        </el-form-item>
        <!--通道选择-->
        <el-form-item label="通道" prop="tdGuid" class="el-form-search-item">
          <el-select v-model="queryParams.tdGuid" placeholder="请选择设备通道" clearable filterable>
            <el-option v-for="channel in channelList" :key="channel.tdGuid" :label="channel.channelName"
              :value="channel.tdGuid" />
          </el-select>
        </el-form-item>

        <el-form-item label="查询时间" prop="dateTime">
          <!-- <el-date-picker v-model="queryParams.dateTime" type="dateTimerange" unlink-panels range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"
            value-format="yyyy-MM-dd HH:mm:ss" size="small" style="width: 340px">
          </el-date-picker> -->

          <el-date-picker v-model="queryParams.dateTime" type="datetimerange" unlink-panels range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"
            value-format="yyyy-MM-dd HH:mm:ss" size="small" style="width: 340px">
          </el-date-picker>
        </el-form-item>

        <el-form-item class="el-form-search-item">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <el-table border :height="tableHeight" v-loading="loading" :data="eventList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="num" />
      <!-- <el-table-column label="通道编号" align="center" prop="tdGuid" /> -->
      <el-table-column label="事件记录时间" align="center" prop="timeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.timeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="事件数据类型" align="center" prop="waveType" />
      <el-table-column label="波形数据" align="center" prop="imagePath" />
      <el-table-column label="事件路径" align="center" prop="wavePath" />
      <el-table-column label="事件波形标志" align="center" prop="mark" />
      <el-table-column label="事件类型" align="center" prop="eventType" />
      <el-table-column label="特征相位" align="center" prop="iPase" />
      <el-table-column label="触发相位" align="center" prop="nPase" />
      <el-table-column label="事件开始时间" align="center" prop="sTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="事件结束时间" align="center" prop="eTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.eTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="持续时间" align="center" prop="keepTime" />
      <el-table-column label="特征幅值" align="center" prop="evtVal" />
      <el-table-column label="额定电压" align="center" prop="eddy" />
      <el-table-column label="规约类型" align="center" prop="gylx" />
      <el-table-column label="起始时间" align="center" prop="sTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="eTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.eTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- 事件记录时间 -->
      <el-table-column prop="timeDate" label="事件记录时间" width="120" sortable>
        <template #default="scope">
          {{ parseTime(scope.row.timeDate, '{y}-{m}-{d}') }}
        </template>
      </el-table-column>

      <!-- 开始时间 -->

      <!-- 规约类型 -->
      <el-table-column prop="gylx" label="规约类型" width="120" />

      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['event:event:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['event:event:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改event对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false"
      v-dialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="设备" porp="device" class="el-form-search-item">
              <el-select v-model="form.deviceId" style="width: 100%;" placeholder="请选择设备类型" clearable filterable
                @change="FormChannelList">
                <el-option v-for="device in deviceList" :key="device.zhGuid" :label="device.devName"
                  :value="device.zhGuid" />
              </el-select>
            </el-form-item>
          </el-col>
          <!--通道选择-->
          <el-col :span="12">
            <el-form-item label="通道" porp="channel" class="el-form-search-item">
              <el-select v-model="form.tdGuid" style="width: 100%;" placeholder="请选择设备类型" clearable filterable>
                <el-option v-for="channel in channelList" :key="channel.tdGuid" :label="channel.channelName"
                  :value="channel.tdGuid" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="事件类型" prop="eventType">
              <el-select style="width: 100%;" v-model="form.eventType" placeholder="请选择事件类型" clearable>
                <el-option label="骤升" value="骤升" />
                <el-option label="骤降" value="骤降" />
                <el-option label="中断" value="中断" />
                <el-option label="录波" value="录波" />
                <el-option label="SOE" value="SOE" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="记录时间" prop="timeDate">
              <el-date-picker style="width: 100%;" clearable size="small" v-model="form.timeDate" type="date"
                value-format="yyyy-MM-dd" placeholder="选择事件记录时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="事件路径" prop="wavePath">
              <el-input v-model="form.wavePath" placeholder="请输入事件路径" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="波形标志" prop="mark">
              <el-input v-model="form.mark" placeholder="请输入事件波形标志" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="波形数据" prop="imagePath">
              <el-input v-model="form.imagePath" placeholder="请输入波形数据" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="特征相位" prop="iPase">
              <el-input v-model="form.iPase" placeholder="请输入特征相位" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="触发相位" prop="nPase">
              <el-input v-model="form.nPase" placeholder="请输入触发相位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="sTime">
              <el-date-picker style="width: 100%;" clearable size="small" v-model="form.sTime" type="date"
                value-format="yyyy-MM-dd" placeholder="选择开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="结束时间" prop="eTime">
              <el-date-picker style="width: 100%;" clearable size="small" v-model="form.eTime" type="date"
                value-format="yyyy-MM-dd" placeholder="选择结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="持续时间" prop="keepTime">
              <el-input v-model="form.keepTime" placeholder="请输入持续时间" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="特征幅值" prop="evtVal">
              <el-input v-model="form.evtVal" placeholder="请输入特征幅值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="额定电压" prop="eddy">
              <el-input v-model="form.eddy" placeholder="请输入额定电压" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="规约类型" prop="gylx">
              <el-input v-model="form.gylx" placeholder="请输入规约类型" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEvent, getEvent, delEvent, addEvent, updateEvent } from "@/api/event/event";
import { mapState } from "vuex";
export default {
  name: "Event",
  data() {
    return {
      // 表格高度
      tableHeight: "calc(100vh - 200px)",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //日期选择框快捷键
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // event表格数据
      eventList: [],
      channelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        orderByColumn: "create_time",
        deviceId: '',
        dateTime: [],
        isAsc: "desc",
        tdGuid: null,
        timeDate: null,
        wavetype: null,
        wave: null,
        wavePath: null,
        mark: null,
        eventType: null,
        iPase: null,
        nPase: null,
        sTime: null,
        eTime: null,
        keepTime: null,
        evtVal: null,
        eddy: null,
        gylx: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();

    let obj = this.$store.state.filterConditions.filterConditions
    console.log("obj", obj);

    if (obj) {
      this.queryParams.deviceId = obj.device
      this.queryParams.dateTime = obj.dateTime
      let device = this.deviceList.find(f => f.zhGuid == this.queryParams.deviceId)
      if (device) {
        this.channelList = device.channelList;
        this.queryParams.tdGuid = obj.channel
      }
    }
  },
  watch: {
    queryParams: {
      handler(newForm, oldForm) {
        let obj = Object.assign({}, newForm)
        obj.channel = obj.tdGuid
        obj.device = obj.deviceId
        obj.dateTime = obj.dateTime || []
        delete obj.tdGuid
        delete obj.deviceId
        this.$store.commit("UPDATE_FILTER_CONDITIONS", obj);
        console.log(this.$store.state.filterConditions.filterConditions);
      },
      deep: true,
    },
  },
  computed: {

    ...mapState({
      deviceList: state => state.user.deviceList, // 从 Vuex 中获取 deviceList
    })
  },
  methods: {
    setChannelList(id) {
      let device = this.deviceList.find(f => f.zhGuid == id)
      if (device) {
        this.channelList = device.channelList;
        this.queryParams.tdGuid = device.channelList[0]?.tdGuid;
      }
    },
    FormChannelList(id) {
      let device = this.deviceList.find(f => f.zhGuid == id)
      if (device) {
        this.channelList = device.channelList;
        this.form.tdGuid = device.channelList[0]?.tdGuid;
      }
    },
    /** 查询event列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.dateTime && this.queryParams.dateTime.length > 1) {
        const [startTime, endTime] = this.queryParams.dateTime
        this.queryParams.sTime = startTime
        this.queryParams.eTime = endTime
      } else {
        this.queryParams.sTime = null
        this.queryParams.eTime = null
      }
      listEvent(this.queryParams).then(response => {
        this.eventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tdGuid: null,
        timeDate: null,
        wavetype: null,
        wave: null,
        wavePath: null,
        mark: null,
        eventType: null,
        iPase: null,
        nPase: null,
        sTime: null,
        eTime: null,
        keepTime: null,
        evtVal: null,
        eddy: null,
        gylx: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加事件信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const tdGuid = row.tdGuid || this.ids
      getEvent(tdGuid).then(response => {
        this.form = response.data;
        let device = this.deviceList.find(f => f.zhGuid == this.form.deviceId)
        if (device) {
          this.channelList = device.channelList;
          this.form.tdGuid = device.channelList[0]?.tdGuid;
        }
        this.open = true;
        this.title = "修改事件信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEvent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEvent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$modal.confirm('是否确认删除记录？').then(function () {
        return delEvent(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('event/event/export', {
        ...this.queryParams
      }, `event_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
